package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseArray;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseModel;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.BaseParams;
import com.isunland.managesystem.entity.DataStatus;
import com.isunland.managesystem.entity.SimpleListParams;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/13 15:44
 */
public abstract class SimpleListFragment<T extends BaseModel> extends BaseListFragment {
  protected static final int REQUEST_ADD = 100;
  protected static final int REQUEST_DETAIL = 200;
  public Class<? extends BaseArray> mClassOriginal;
  protected ArrayList<T> mList;
  protected ArrayList<T> mListOriginal;//全部数据
  protected ArrayList<T> mListTemp;//临时数据.
  protected BaseAdapter mAdapter;
  protected ParamsNotEmpty mParamsNotEmpty;
  private String mTitle;
  private String mUrl;
  private String mDataStatus;
  private Class<? extends BaseVolleyActivity> mClassDetailActivity;

  public ParamsNotEmpty getmParamsNotEmpty() {
    return mParamsNotEmpty;
  }

  @SuppressWarnings("unused") public void setmParamsNotEmpty(ParamsNotEmpty mParamsNotEmpty) {
    this.mParamsNotEmpty = mParamsNotEmpty;
  }

  @Override public void initData() {
    super.initData();
    SimpleListParams params = initParams();
    mTitle = params.getTitle();
    mUrl = params.getUrl();
    mDataStatus = params.getDataStaus();
    mParamsNotEmpty = params.getParamsNotEmpty();
    mClassOriginal = params.getClassOriginal();
    mClassDetailActivity = params.getClassDetailActivity();
    mList = new ArrayList<>();
    mListOriginal = new ArrayList<>();
    mListTemp = new ArrayList<>();
    mAdapter = initAdapter(mActivity, mList);
    if (mParamsNotEmpty == null) mParamsNotEmpty = new ParamsNotEmpty();
  }

  protected abstract SimpleListParams initParams();

  protected abstract BaseAdapter initAdapter(BaseVolleyActivity context, ArrayList<T> list);

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(mTitle);
  }

  @Override public void resolveResponse(String json) {
    //noinspection unchecked
    ArrayList<T> temp = (ArrayList<T>) new Gson().fromJson(json, mClassOriginal).getRows();
    mListOriginal.clear();
    mList.clear();
    mListTemp.clear();

    mListOriginal.addAll(temp);
    mList.addAll(temp);
    mListTemp.addAll(temp);
    //不再initView中设置,因为adapter的isEmpty是false的.会没有dialogLoading.
    setListAdapter(mAdapter);
    setAdapter();
  }

  protected void setAdapter() {
    if (mListTemp == null) return;
    mList.clear();
    mList.addAll(mListTemp);
    mAdapter.notifyDataSetChanged();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    if (MyStringUtil.equals(mDataStatus, DataStatus.NEW) || MyStringUtil.equals(mDataStatus,
        DataStatus.NEWBACK)) {
      inflater.inflate(R.menu.menu_add, menu);
    }
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_add:
        //BaseParams params = new BaseParams();
        mBaseParams.setType(BaseParams.TYPE_ADD);
        //添加时时清空item.
        //noinspection unchecked
        mBaseParams.setItem(null);
        BaseVolleyActivity.newInstance(this, mClassDetailActivity, mBaseParams, REQUEST_ADD);
        return true;

      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return mUrl;
  }

  @Override public HashMap<String, String> getparameters() {
    return mParamsNotEmpty.getParams();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    if (mAdapter == null) return;
    if (position == 0) return;
    //noinspection unchecked
    T item = (T) mAdapter.getItem(position - l.getHeaderViewsCount());
    //SimpleListDetailParams params = new SimpleListDetailParams();
    //noinspection unchecked

    //noinspection unchecked
    mBaseParams.setItem(item);
    mBaseParams.setType(BaseParams.TYPE_DETAIL);
    BaseVolleyActivity.newInstance(this, mClassDetailActivity, mBaseParams, REQUEST_DETAIL);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_ADD) volleyPost();
    if (requestCode == REQUEST_DETAIL) volleyPost();
  }
}
